package com.czl.module_storehouse.mvp.model;

import android.util.Log;

import com.czl.base.data.bean.tengyun.BorrowBean;
import com.czl.base.data.net.RetrofitClient;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_storehouse.BuildConfig;
import com.czl.module_storehouse.api.ApiService;
import com.czl.module_storehouse.bean.BorrowInfoBean;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONObject;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author by xubin,
 * @date on 2022/2/10.
 * PS: Not easy to write code, please indicate.
 */
public class BorrowModel extends BasicDataModel {

    public Observable<HttpResponse<ListBean<BorrowBean>>> getNeedDealtBorrowList(int pageNo) {
        JSONObject object = new JSONObject();
        try {
            object.put("companyId", getManageCompanyId());
            object.put("pageNo", pageNo);
            JSONArray array = new JSONArray();
            array.put(1);
            array.put(2);
            object.put("classStatus", array);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .getNeedDealtBorrowList(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<ListBean<BorrowBean>>> getDealtBorrowList(int pageNo) {
        JSONObject object = new JSONObject();
        try {
            object.put("storehouseId", getStorehouseId());
            object.put("pageNo", pageNo);
            object.put("pageSize", 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .getDealtBorrowList(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<Object>> addBorrowInfo(BorrowInfoBean bean) {
        bean.setBorrowCompanyId(getManageCompanyId());
        bean.setStorehouseId(getStorehouseId());
        bean.setBorrowCompanyName(getManageCompanyName());
        bean.setBorrowCompanyShortName(getStoreCompanyShortName());
        bean.setOperaterId(getUserId());
        bean.setOperaterName(getTName());
        Gson gson = new Gson();
        String json = gson.toJson(bean);
        if (BuildConfig.DEBUG) {
            Log.e(TAG, "BorrowInfoBean :" + json);
        }
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .addBorrowInfo(RequestBody.create(mediaType, json));
    }
}
